package com.levi.array;

import java.io.*;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * @author shentong
 * @date 2021/8/22 16:45
 * @Description 稀疏数组  实现 五子棋 存盘和读取
 */
public class SparseArray {
    private static final String FILEPATH = "E:/IdeaProjects/data-structures-and-algorithms/files/sparseArr.txt";
    public static void main(String[] args) {
        //算出一共有几个有效数据
        var sum = 0;
        //1.创建一个11*11的数组，并赋值
        int[][] originArr = new int[11][11];
        originArr[1][2] = 1;
        originArr[2][3] = 2;
        System.out.println("原始二维数组为:----------");
        for (int[] ints : originArr) {
            for (int anInt : ints) {
                System.out.printf("%d\t",anInt);
            }
            System.out.println();
        }
        //2.把原始数组转为稀疏数组,
        for (int[] row : originArr) {
            for (int i : row) {
                if (i != 0) {
                    sum ++;
                }
            }
        }
        int[][] sparseArr = new int[sum+1][3];
        sparseArr[0][0] = originArr.length;
        sparseArr[0][1] = originArr[0].length;
        sparseArr[0][2] = sum;


        //声明sparseArr的行数  从下表为1的行开始
        var count = 1;
        for (int i = 0; i < originArr.length; i++) {
            for (int j = 0; j < originArr[0].length; j++) {
                if (originArr[i][j] != 0) {
                    sparseArr[count][0] = i;//行
                    sparseArr[count][1] = j;//列
                    sparseArr[count][2] = originArr[i][j];//值
                    count++;
                }
            }
        }
        System.out.println("把二维数组转为稀疏数组为:----------------");
        for (int[] ints : sparseArr) {
            for (int anInt : ints) {
                System.out.printf("%d\t",anInt);
            }
            System.out.println();
        }
        //3.存盘
        saveToDisk(sparseArr);
        //4.读取稀疏数组
        int[][] sparseArr1 = readFromDisk();
        System.out.println("读取的稀疏数组是:------------");
        for (int[] ints : sparseArr1) {
            for (int anInt : ints) {
                System.out.printf("%d\t",anInt);
            }
            System.out.println();
        }
        //5.把稀疏数组转为二维数组
        int[][] originArr1 = sparseToOrigin(sparseArr1);
        //打印出来
        System.out.println("把稀疏数组转换为二维数组为:");
        for (int[] ints : originArr1) {
            for (int anInt : ints) {
                System.out.printf("%d\t",anInt);
            }
            System.out.println();
        }
    }

    /**
     * 把稀疏数组从磁盘读取出来
     */
    private static int[][] readFromDisk() {
        //算出来文件中的数组有多少行
        var sum = 0;
        File file = new File(FILEPATH);
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            while (reader.readLine() != null) {
                sum++;
            }
            reader.close();
            System.out.printf("文件中的数组一共有%d\n行",sum);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int[][] sparseArr = new int[sum][3];
        //开始读取  ，用Scaner读取
        try {
            Scanner scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                for (int i = 0; i < sparseArr.length; i++) {
                    //读取一行，分成字符数组然后逐个取
                    String[] lines = scanner.nextLine().split("\t");
                    for (int j = 0; j < lines.length; j++) {
                        sparseArr[i][j] = Integer.parseInt(lines[j]);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return sparseArr;
    }

    /**
     * 存盘
     */
    private static void saveToDisk(int[][] sparseArr) {
        try {
            File file = new File(FILEPATH);
            FileWriter out = new FileWriter(file);
            for (int i = 0; i < sparseArr.length; i++) {
                for (int j = 0; j < sparseArr[0].length; j++) {
                    out.write(sparseArr[i][j]+"\t");
                }
                out.write("\r\n");
            }
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 把稀疏数组转为二维数组
     * @param sparseArr
     * @return
     */
    public static int[][] sparseToOrigin(int[][] sparseArr){
        int[][] originArr1 = new int[sparseArr[0][0]][sparseArr[0][1]];
        for (int i = 1; i < sparseArr.length; i++) {
            originArr1[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }
        return originArr1;
    }
}
